import { IUseTemplate, IWord, TestTemplate, TestTemplates } from "@/store/study";
import { deepClone, randomArray, isString, lodash } from "@/utils";

/**
 * 填充测试数组
 * @param {IWord[]} targetList 填充目标数组
 * @param {IWord[]} useList 填充消耗耗材数组
 * @return 
 */ 
export function fillTestData(targetList: IWord[], useList?: IWord[]) {
	targetList.forEach(item => {
		const unStepData = useList.filter(wordInfo => {
			return item.Word.toLowerCase() !== wordInfo.Word.toLowerCase()
		})
		item.testShuffle = unStepData.map((shuffle: IWord) => {
			return { ...shuffle, UseTemplates: [] };
		})
	})
}

/**
 * 初始化配置模板数据
 * @param {TestTemplate[]} tempConfig 
 * @param {{ TotalCount: number; ErrorCount: number }} options 
 * @return 
 */ 
export function initUseTemplateConfig(tempConfig: TestTemplate[], options: { TotalCount: number; ErrorCount: number }) {
	return (tempConfig || []).map(temp => {
		return {
			template: { ...temp },
			...options
		}
	})
}

/**
 * 获取模版类型
 * @param {TestTemplate | IUseTemplate} template 
 * @return 
 */ 
export function getTemplateType(template: TestTemplate | IUseTemplate): string {
	if (lodash.has(template, 'template')) {
		return lodash.get(template, 'template.type', '')
	}
	
	return lodash.get(template, 'type', '')
}

/**
 * 校验是否满足测试模板条件
 * @param {TestTemplate | TestTemplate[]} originTemplates 
 * @param {TestTemplate | IUseTemplate} validTemplate 
 * @return 
 */ 
export function hasTemplate(originTemplates: TestTemplate | TestTemplate[], validTemplate: TestTemplate | IUseTemplate | string): boolean {
	const type: string = isString(validTemplate) ? validTemplate as string : getTemplateType(validTemplate as any);
	if (Array.isArray(originTemplates)) {
		return originTemplates.some(item => item.type === type);
	}
	return originTemplates.type === type;
}

export type TestFoundResult = {
	/** 是否存在测试项 */
	found: boolean;
	/** 测试项 */
	attrs?: {
		current: IWord,
		type: TestTemplate
	}
}
	
/** hooks: 单词测试工具 */
export default function useTest() {

	// 测试模板使用顺序
	const testTemplateSort = [];
	
	/** 初始化测试模板使用顺序 */
	function initTestSort() {
		for (let key in TestTemplates) {
			testTemplateSort.push(key);
		}
	}
	
	/** 获取测试词汇的使用结构 */
	function findCurrentByTemp(temps: any[], type?: string): TestFoundResult {
		if (temps.length === 1) {
			return {
				found: true,
				attrs: {
					current: deepClone(temps[0]),
					type: deepClone(TestTemplates[type])
				}
			}
		} else {
			return {
				found: true,
				attrs: {
					current: deepClone(randomArray(temps)[0]),
					type: deepClone(TestTemplates[type])
				}
			}
		}
	}

	/** 获取测试词汇 */
	function findCurrent(inWordData: IWord[], isSort: boolean = true): TestFoundResult {
		if (isSort) {
			for (var i = 0; i < testTemplateSort.length; i++) {
				const type = testTemplateSort[i];
				const temps = inWordData.filter(wd => wd.UseTemplates.some(ut => ut.template.type === type && ut.TotalCount > 0));
				if (temps.length > 0) {
					return findCurrentByTemp(temps, type);
				}
			}
		} else {
			const temps = inWordData.filter(wd => wd.UseTemplates.some(ut => ut.TotalCount > 0));
			
			if (temps.length > 0) {
				const tmp = temps[0].UseTemplates.find(ut => ut.TotalCount > 0);
				return findCurrentByTemp([temps[0]], tmp.template.type);
			}
		}
		
		return { found: false };
	}
	
	/** 判断模板列表中是否包含Syllable */
	function isExistSyllableTemp(inTemplates: TestTemplate[]){
		return inTemplates.some(temp => temp.type === TestTemplates.SPELL_SYLLABLE.type);
	}
	
	/** 使用模板规范化 */
	function templatesNormalize(inTemplates: TestTemplate[], inWordData: IWord): TestTemplate[] {
		if (inTemplates.some(temp => temp.type === TestTemplates.SPELL_SYLLABLE.type)) {
			const isNotType = (type: string) => type !== TestTemplates.SPELL_SYLLABLE.type;
			if (!/[^a-zA-Z]/.test(inWordData.Word) && inWordData.Syllable) {
				const condition = inWordData.Syllable.includes('/') && inWordData.Syllable.replace(/\//g,'') === inWordData.Word;
				if (!condition) {
					return inTemplates.filter(temp => isNotType(temp.type))
				}
			} else {
				return inTemplates.filter(temp => isNotType(temp.type))
			}
		}
		return inTemplates;
	}
	
	// 初始化
	initTestSort();
	
	return {
		findCurrent,
		fillTestData,
		initUseTemplateConfig,
		hasTemplate,
		getTemplateType,
		templatesNormalize,
		isExistSyllableTemp
	}
}